package com.twu.biblioteca.models;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ema93sh
 * Date: 8/9/13
 * Time: 4:18 PM
 */
public class Book {
    private String title;
    private List<String> authors;
    boolean reserved;
    private double price;
    private int noOfCopies;
    private int noOfCopiesAvailable;

    public Book(String title, int noOfCopies)
    {
        this.title = title;
        this.noOfCopies = noOfCopies;
        this.noOfCopiesAvailable = noOfCopies;
        authors = new ArrayList<String>();
        reserved = false;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean addAuthor(String author) {            // return true on success
        if (authors.contains(author))
        {
            return false;
        }
        authors.add(author);
        return true;

    }

    public void setPrice(double price) {
        this.price = price;
    }

    public boolean IsReserved() {
        this.reserved = (noOfCopiesAvailable <= 0);
        return reserved;
    }

    public void setReserved(boolean reserved) {
        this.reserved = reserved;
    }

    @Override
    public boolean equals(Object otherObject)
    {
        if (otherObject == this)
        {
            return true;
        }

        if (!(otherObject instanceof Book))
        {
            return false;
        }

        Book book = (Book) otherObject;

        return book.title.equalsIgnoreCase(this.title);
    }


    public void setNoOfCopies(int noOfCopies) {
        this.noOfCopies = noOfCopies;
    }

    public int getNoOfCopies() {
        return noOfCopies;
    }

    public int getNoOfCopiesAvailable() {
        return noOfCopiesAvailable;
    }

    public void reserve() {
        this.noOfCopiesAvailable -= 1;
    }
}
